********************************************************************************
*																				*
*					IZA Covid Adlisting graph Replication in Final Report	    *
*																				*
********************************************************************************
	
* 	Note: 	
*				The dofile does not require setting up a current directory
	
*				Directly executing the dofile after extracting contents from main ZIP
*				file will display all results
	
*				Prepared using: Stata/SE 17.0 for Windows (64-bit) (Revision 6 Apr 2022)


* Description:
*				The provided code reproduces the figure in blog.

*				The code is ordered in the same manner as numbers appear in text.
*				To make it easy to read, each code block is numbered and header
*				is provided to match with that within the blog text.

	
********************************************************************************		

* 								Visualization 	

********************************************************************************

*Covid-19 cases information
{	
	clear all
	tempfile master
	save `master', emptyok
	
	* Setting URL path from where the datasets can be acquired 
	local url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports"
	local startdate = date("02-01-2020", "MDY")
	local enddate = date("10-01-2022", "MDY")

	forvalues i = `startdate'(1)`enddate' {
	local date: display %tdNN-DD-CCYY `i'-1
	display "`date'"

	capture noisily insheet using "`url'/`date'.csv", clear

	capture noisily rename incidence_rate incident_rate
	capture noisily rename provincestate province_state

	capture noisily rename lastupdate last_update
	capture noisily replace last_update = "`date'" if missing(last_update)

	capture noisily rename latitude lat
	capture noisily rename longitude long_

	capture noisily rename casefatality_ratio case_fatality_ratio
	capture noisily tostring case_fatality_ratio, replace format(%24.0g)

	capture noisily rename countryregion country_region
	capture noisily keep if country_region == "Pakistan"

	capture noisily append using `master'
	capture noisily save `"`master'"', replace
	}

	* Converting case_fatality_ratio to numeric
	destring case_fatality_ratio, replace

	* Compressing the dataset
	compress

	* Formatting the last_update variable to non-missing 
	* and readable format
	generate double last_update_n = clock(last_update, "YMD hms"), after(last_update)
	format last_update_n %tc

	generate temp = last_update if missing(last_update_n)

	generate double time_sec = clock(temp, "YMD#hms")
	replace temp = "" if !missing(time_sec)

	generate double time_min = clock(temp, "MD20Y hm")

	replace last_update_n = time_min if !missing(time_min)
	replace last_update_n = time_sec if !missing(time_sec) 

	drop temp time* last_update

	rename last_update_n last_update 


	* Filling in missing value so that unique identifier can be formed
	replace combined_key = "Pakistan" if missing(combined_key)
	replace lat = 30.3753 if missing(lat) 
	replace long_ = 69.3451 if missing(long_)

	duplicates tag combined_key last_update, gen(dup)
	
	if dup > 0 {
		
		bysort  last_update combined_key  (incident_rate) : assert incident_rate[1] == incident_rate[_N]
	}
	
	duplicates drop last_update combined_key,force
	

	* Unique identifier
	isid last_update combined_key
	
	bysort last_update: egen total_cases_daily = total(confirmed)
	bysort last_update: keep if _n == 1

	replace total_cases_daily = 16 in 7

	generate new_cases = total_cases_daily[_n] - total_cases_daily[_n-1]

	replace new_cases = total_cases_daily if _n == 1
		
	keep last_update total_cases_daily new_cases

	generate monthly = mofd(dofc(last_update))
	sort monthly, stable

	collapse (lastnm) total_cases_daily (sum) new_cases, by(monthly)	
	
	
}

	drop    if monthly == 753
	tempfile covid_cases 
	save `covid_cases'

	
	*generating the adlisting rate
	use 				"./Data/firm_survey_relisting.dta", clear
	
	distinct       firm_id
	loc            firm_nb = `r(ndistinct)'
  
	duplicates 		tag firm_id cycle, gen(dup)
	
	sum             dup if dup ==0
	loc             firm_rd = `r(N)'
  
	duplicates    drop monthly_date_relist,force
	keep 		  monthly_date_relist number_ads_listed_m  number_firms_m
	gen 		  prop_ads=round((number_ads_listed_m/ number_firms_m)*100,0.01)
	format         monthly_date_relist %td
	gen			   sdate = string(monthly_date_relist , "%tdD_M_CY")
	labmask 	   monthly_date_relist, values(sdate) 
	sort           monthly_date_relist
				
	
	tempfile 	  firm
	save 		 `firm'

	clear
	set			 obs 54
		
	local 		i = 0
	local 		cmd generate
	forvalues 	month = 1/12{
		
					forvalues 	year = 2019(1)2022{
					`cmd' datestr = "1/`month'/`year'" in `=`++i''
					local cmd replace
		}
	}
	
	gen 		double subdate= date( datestr, "DMY")
	format 		subdate %td
	sort 		subdate
	keep 		if subdate >=td(01oct2019) & subdate <= td(01aug2022)

	drop 		datestr
	ren 		subdate monthly_date_relist
	
	merge 		1:1 monthly_date_relist using `firm', nogen
	
	generate 	srno = _n
	generate 	monthly = mofd(monthly_date_relist)
	
	joinby 		monthly using `covid_cases', unmatched(both)
	drop 		if missing(monthly_date_relist)
		
	drop 		if monthly_date_relist > td(01aug2022)
		
	* Relisting plot
	twoway 		(scatteri 0 4 0 35 8.5 36 8.5 4, recast(area) lwidth(0) color(orange%12)) ///
				(scatteri 9 17.25 "COVID-19", ms(i) mlabcolor("225 87 89") mlabpos(0) mlabsize(3)) ///
				(pcarrowi 8.7 29.5 8.7 5, lcolor("225 87 89") mcolor("225 87 89")) ///
				(pcarrowi 8.7 5 8.7 36, lcolor("225 87 89") mcolor("225 87 89"))  ///
				(bar prop_ads srno, barwidth(0.6) bcolor("31 119 180") blcolor(black)) /// 
				(scatteri 0 4 0 10 8.5 10 8.5 4 , recast(area) color("gs12%60") lwidth(0)) ///
				(scatteri 0 30.3 0 32.75 8.5 32.75 8.5 30.3 , recast(area) color("gs12%60") lwidth(0)) ///
				(scatteri 12 6 "", ms(i) mlabcolor(red)) ///
				(scatteri 0 13.75 0 14.25 8.5 14.25 8.5 13.75, recast(area) color("gs12%60") lwidth(0)) ///
				(scatteri 0 16.75 0 17.25 8.5 17.25 8.5 16.75, recast(area) color("gs12%60") lwidth(0)) ///	
				(scatteri 0 18.75 0 19.25 8.5 19.25 8.5 18.75, recast(area) color("gs12%60") lwidth(0)) ///
				(scatteri 8.5 5 0 5, recast(line) lcolor(red)) ///			
				(scatteri 8.1 11, msymbol(O) msize(2) mlwidth(1.25) mcolor("44 160 44") mlabpos(0)) ///
				(scatteri 7.5 11, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 12, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 13, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 14, msymbol(square) msize(4) mcolor(orange*0.12) mlabpos(0)) ///
				(scatteri 7.5 14, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 15, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 16, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 17, msymbol(square) msize(4) mcolor(orange*0.12) mlabpos(0)) ///
				(scatteri 7.5 17, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 18, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 19, msymbol(square) msize(4) mcolor(orange*0.12) mlabpos(0)) ///
				(scatteri 7.5 19, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("214 39 40") mlabpos(0)) ///
				(scatteri 8.1 20, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("214 39 40") mlabpos(0)) ///
				(scatteri 7.5 20, msymbol(O) msize(2) mlwidth(1.25) mcolor("44 160 44") mlabpos(0)) ///
				(scatteri 7.5 21, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 8.1 22, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("214 39 40") mlabpos(0)) ///
				(scatteri 7.5 22, msymbol(O) msize(2) mlwidth(1.25) mcolor("44 160 44") mlabpos(0)) ///
				(scatteri 7.5 23, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 24, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 25, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 26, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 27, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 28, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 29, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 30, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 31, msymbol(square) msize(4) mcolor(orange*0.12) mlabpos(0)) ///
				(scatteri 7.5 31, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0))  ///
				(scatteri 7.5 32, msymbol(square) msize(4) mcolor(orange*0.12) mlabpos(0)) ///
				(scatteri 7.5 32,  msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 33, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 34, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///
				(scatteri 7.5 35, msymbol(X) msize(3.75) mlwidth(1.25) mcolor("255 127 14") mlabpos(0)) ///	
				(scatteri 11.7 0 "{bf}Of the firms which were approached, how many of them listed Ads?", ms(i) mlabpos(1) mlabsize(3.75) mlabcolor(gs3)) /// 
				(scatteri 11.1 0 "Visual is based on (No. of ads posted / No. of firms called)*100", ms(i) mlabpos(1) mlabsize(3.75) mlabcolor(gs3)) ///
				, ///
				xscale(off noextend) ///
				xlabel(, nogrid) ///
				ylabel(0(2.5)7.5, nogrid labsize(2.5)) xtitle("") ytitle("") ///
				legend(order(25 "Strict Lockdown" 19 "Smart Lockdown" 27 "Strict Lockdown" "Lifted" 10 "No Data Available") bmargin(t=9) ring(0) col(1) row(1) pos(10) symysize(3) symxsize(3) region(fcolor(white%0)) ) plotregion(margin(none)) graphregion(margin(r=10 l=-3)) xscale(noline) yscale(noline) ///
				scheme(white_tableau) name(relisting, replace)
			

		
		
   * Covid Plot
		#delimit ;
			twoway (bar new_cases srno, bcolor("135 10 117") blcolor(black) blwidth(0.1) barwidth(0.6)
			xtitle("")
			ylabel(0 "0" 50000 "5" 100000 "10" 150000 "15" 200000 " ", nogrid tlcolor(white) labsize(3) labcolor(white) angle(horizontal))
			xlabel(, nogrid)
			xscale(noextend off)
			yscale(noline)
			fysize(12))
			(scatteri 110000 0 "COVID-19", ms(i) mlabpos(1) mlabsize(2.5) mlabcolor(red))
			(scatteri 75000 0 "Monthly New Cases", ms(i) mlabpos(1) mlabsize(2.5) mlabcolor(red))
			(scatteri 40000 0 "(in 10,000's)", ms(i) mlabpos(1) mlabsize(2.5) mlabcolor(red))
			(scatteri 170000 9 "14.3", ms(i) mlabpos(0) mlabsize(2.5) mlabcolor(black))			
			(scatteri 175000 19 "15.3", ms(i) mlabpos(0) mlabsize(2.5) mlabcolor(black))			
			(scatteri 160000 23 "13", ms(i) mlabpos(0) mlabsize(2.5) mlabcolor(black))			
			(scatteri 160000 28 "12.9", ms(i) mlabpos(0) mlabsize(2.5) mlabcolor(black))			
			(scatteri 160000 33 ".54", ms(i) mlabpos(0) mlabsize(2.5) mlabcolor(black))			
			,			
			name(covid, replace) 
			plotregion(margin(none)) graphregion(margin(r=10 l=-3))
			scheme(white_tableau) legend(off)
		;
		#delimit cr
		
		
	* Generating the X-axis
		#delimit ;
			twoway (bar new_cases srno, bcolor("135 10 117%0") barwidth(0.6)
			xtitle("")
			ylabel(0 " " 50000 "5" 100000 "10" 150000 "15", nogrid tlcolor(white) labsize(2.5) labcolor(white) angle(horizontal))
			
			xlabel(	1 "Oct '19" 3 "Dec '19" ///
					5 "Feb '20" ///
					7 "Apr '20" 9 "Jun 20" ///
					11 "Aug '20" 13 "Oct '20" ///
					15 "Dec '20" ///
					17 "Feb '21" 19 "Apr '21" ///
					21 "Jun '21" ///
					23 "Aug '21" ///
					25 "Oct '21" 27 "Dec '21" 29 "Feb '22" 31 "Apr '22" 33 "Jun '22" 35 "Aug '22", labsize(2.5) nogrid)
			xscale(noextend)
			ytitle("")
			yscale(reverse noline)
			fysize(2)),
			name(x_axis, replace) 
			plotregion(margin(none)) graphregion(margin(r=10 l=-3))
			scheme(white_tableau) legend(off)
		;
		#delimit cr
		
		
	* Combining graphs
		#delimit ;
			graph combine relisting covid x_axis,
			cols(1)
			imargin(0 0 2 0) xcommon
			graphregion(margin(l=5 r=5))
			note("Note: The red vertical line indicates the emergence of Covid-19 in the country." "          The gap in the bar graph marked by gray shade indicates the time period when ads were not being collected." "          Strict Lockdown: Complete restriction in all areas of district Lahore." "          Smart Lockdown: Controlled entry and exit in areas of district Lahore. Identified as areas with high positivity rate of COVID-19." "          Strict Lockdown Lifted: All restrictions lifted with SOP's in effect." ///
			" " ///
			"          N = `firm_rd' (Firms x Call Rounds)" "          Firms = `firm_nb' ", size(*.7) margin(t+2)) ///
			scheme(white_tableau)
			l1("Vacancy" "posting" "rate (%)", size(2.25) orient(horizontal)) ///
		;
		#delimit cr
		
		graph 	export "./Results/vacancy_posting_rate_covid_v2_aug.png", as(png) width(3840) replace
		graph 	export "./Results/vacancy_posting_rate_covid_v2_aug.eps", as(eps) replace
